<?php

namespace Admin\Controller;
use User\Api\UserApi;
use Think\Log;

class ObdController extends AdminController {
    public function index($status=''){
        if(!empty($status)){
            $map['status'] = $status;
        }else{
            $status = null;
        }
        $keyword = I('title','');
        if(!empty($keyword)){
            $map['obdid'] = array('like','%'.$keyword .'%');
        }
        $model = M('ObdModel');
        $modelCar = M('UserCar');
        $list = $this->lists('Obd', $map);
        foreach($list as &$row){
            unset($where);
            $where['model_id'] = $row['model_id'];
            $res = $model->cache(true)->where($where)->find();
            if($res){
                $row['modelname'] = $res['modelname'];
                $row['manufacturer'] = $res['manufacturer'];
                $row['production_date'] = $res['production_date'];
            }
            unset($where);
            $where['obd_id'] = $row['obdid'];
            $res = $modelCar->where($where)->find();
            if($res){
                $row['carno'] = $res['c_lcn_no'];
            }
        }
        
        int_to_string($list,array('status'=>array(1=>'已启用',-1=>'删除',0=>'未启用',2=>'已激活')));
        $this->assign('status',$status);
        
        $this->assign('_list', $list);
        $this->meta_title = 'OBD设备列表';
        
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->display();
    }
    
    public function add($obdid='',$model_id=''){
        if(IS_POST){
            if(empty($obdid)){
                $this->error("无效的OBD序号");
            }
            if(empty($model_id)){
                $this->error('无效的OBD型号');
            }
            $data['obdid'] = $obdid;
            $data['model_id'] = $model_id;
            $model = M('Obd');
            $ret = $model->add($data);
            if($ret !== false){
                $this->success("保存成功！",U('Obd/index'));
            }else{
                $this->error('数据库写入失败');
            }
            
        }else{
            $model = M('ObdModel');
            $resModels = $model->select();
            $this->assign('models',$resModels);
            $this->meta_title = '新增OBD';
            $this->display();
        }
    }
    
    public function edit($id=''){
        if(empty($id)){
            $this->error('错误的ID');
        }
        if(IS_POST){
            $data['obdid'] = $id;
            $data['model_id'] = I('model_id');
            $model = M('Obd');
            $model->save($data);
            $this->success('操作成功！',U('Obd/index'));
        }else{
            $model = M('ObdModel');
            $resModels = $model->select();
            $this->assign('models',$resModels);

            $model = M('Obd');
            $res = $model->where("obdid=$id")->find();
            $this->assign('obd',$res);
            $this->meta_title = '修改OBD信息';
            $this->display();
        }
    }
    
    public function setStatus($status=''){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['obdid'] =   array('in',$id);
        $data['status'] = $status;
        $model = M('Obd');
        $model->where($map)->save($data);
        $this->success('操作成功！');
    }
    
    public function del($status=''){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['obdid'] =   array('in',$id);
        $model = M('Obd');
        $model->where($map)->delete();
        $this->success('操作成功！');
    }
    
    public function recycle(){
        $map['status'] = -1;
        $keyword = I('title','');
        if(!empty($keyword)){
            $map['obdid'] = array('like','%'.$keyword .'%');
        }
        $model = M('ObdModel');
        $modelCar = M('UserCar');
        $list = $this->lists('Obd', $map);
        foreach($list as &$row){
            unset($where);
            $where['model_id'] = $row['model_id'];
            $res = $model->cache(true)->where($where)->find();
            if($res){
                $row['modelname'] = $res['modelname'];
                $row['manufacturer'] = $res['manufacturer'];
                $row['production_date'] = $res['production_date'];
            }
            unset($where);
            $where['obd_id'] = $row['obdid'];
            $res = $modelCar->where($where)->find();
            if($res){
                $row['carno'] = $res['c_lcn_no'];
            }
        }
        
        $this->assign('_list', $list);
        $this->meta_title = '已删除OBD设备列表';
        
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->display();
    }
}